package com.lmn.oval;

import net.sf.oval.ValidationCycle;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.exception.OValException;

/**
 * @version 4.0
 * @describe: 身份证校验实现
 * @author: houkai
 * @Date: 2019/1/15 13:51
 */
public class CertCheckImpl extends AbstractAnnotationCheck<CertCheck> {

    private static final long serialVersionUID = 1607283161222846335L;

    private boolean notNull;

    @Override
    public void configure(CertCheck certCheck) {
        super.configure(certCheck);
        setNotNull(certCheck.notNull());
    }

    /**
     * 根据身份证前17位数字做模11计算，得出最后一位校验码，再校验最后一位和计算出的结果是否一致
     *
     * @param validatedObject 进入校验器的对象
     * @param valueToValidate 注解当前标注的属性值
     * @return 是否通过验证
     * @throws OValException OValException
     */
    @Override
    public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final ValidationCycle cycle) throws OValException {
        String certNo = (String) valueToValidate;
        if (notNull) {
            if (!CheckUtil.isNotBlank(certNo)) {
                return false;
            }
        } else {
            if (!CheckUtil.isNotBlank(certNo)) {
                return true;
            }
        }
        return IDCardVerify.validateCard(certNo);
    }

    public boolean isNotNull() {
        return notNull;
    }

    public void setNotNull(boolean notNull) {
        this.notNull = notNull;
    }
}
